<template>
  <div id="nursingPdf" >
    <div class="nursingTopHeader" >
      <div class="backTo" @click="back">
        <img src="../../../static/images/back.png" alt="返回" />
      </div>
      <div class="dropDown" v-if="nursingData.length" v-clickoutside="handleClose">
        <p :class="{'up' : isShow}" @click="showNursing">{{nursingStr}}</p>
        <ul :class="['list-link',{'show': isShow}]">
          <li v-for="(item,index) in nursingData" :key="item.value" @click="changeNursing($event)" :data-src="item.MemoryAddress">
            {{item.Abbreviation|filterStr}}
          </li>
        </ul>
      </div>
      <div class="nursingButton">
        <button @click="previous">前一天</button>
        <button @click="current">当天</button>
        <button @click="next">后一天</button>
      </div>
      <div class="nursingAorB">
        <button @click="nursingA">A面</button>
        <button @click="nursingB">B面</button>
      </div>
    </div>
    <iframe :src="src"  width="100%" height="100%"></iframe>
  </div>
</template>
<script>
  import { apiNursingSheet } from '@/api/api'
  import { standardDate } from "@/components/common/date"; //引入时间转换JS
  import axios from 'axios';
  export default {
    data() {
      return {
        nursingData: [],
        src: this.$route.query.http,
        isShow: false,
        nursingStr: '请选择特护单',
        timeData: [],
      }
    },
    mounted(){
      this.getNursingSheetDrop();
    },
    methods: {
      back() {
        if (this.$route.query.state == 1) {
          this.$router.push({
            path: '/inPatients',
            query: { item: 0, department:1}
          })
        } else {
          this.$router.push({
            path: '/nursingListPage/nursingListDetails',
            query: { item: 8}
          })
        }
      },
      getNursingSheetDrop() {
        this.timeData = [];
        apiNursingSheet({
          patientIdIn: this.$route.query.patientId || this.$store.getters.ID 
        })
          .then(res => {
            if (res.Status == 1) {
              this.nursingData 
              for (let item of JSON.parse(res.Data)) {
                if ($.inArray(this.filterDate(item.Abbreviation), this.timeData) ==-1)
                {
                  this.timeData.push(this.filterDate(item.Abbreviation));
                  this.nursingData.push(item);
                }                      
              }
            }
          })
      },
      handleClose() {
        this.isShow = false;
      },
      showNursing() {
        this.isShow = !this.isShow
      },
      changeNursing(event) {
        this.nursingStr = $.trim(event.currentTarget.innerHTML);
        this.isShow = false;
        this.src = axios.defaults.baseURL.replace("api", "") + event.currentTarget.getAttribute("data-src").replace("AppServerWeb", "");
      },
      filterDate(str) {
        if (!str) {
          return ''
        } else {
          return str.substring(str.indexOf('圳') + 1, 28);
        }
      },
      previous() { //前一天
        let srcDate = this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 13);
        let type = this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 2);
        let index = $.inArray(srcDate, this.timeData);
        if (index != -1) {
          if (index == this.timeData.length-2 || index == this.timeData.length - 1) {
            this.$toast.center('该患者没有更早的PDF')
          } else {
            if (type == '01') {
              if (index + 2 > this.timeData.length) {
                index = this.timeData.length
              } else {
                index = index + 2
              }
              if (this.timeData[index].substring(0, 3) == '01_') {
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type); 
              } else if (this.timeData[index - 1].substring(0, 3) == '01_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index - 1].substring(0, 3) == '02_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('02_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }
              
            } else if (type == '02') {
              if (index + 1 > this.timeData.length) {
                index = this.timeData.length
              } else {
                index = index + 1
              }
              if (this.timeData[index].substring(0, 3) == '01_') {
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index - 1].substring(0, 3) == '01_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index - 1].substring(0, 3) == '02_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('02_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }
            } else if (type == '03') {
              if (index + 2 > this.timeData.length) {
                index = this.timeData.length
              } else {
                index = index + 2
              }
              if (this.timeData[index].substring(0, 3) == '03_') {
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index - 1].substring(0, 3) == '03_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index - 1].substring(0, 3) == '04_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('04_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }

            } else if (type == '04') {
              if (index + 1 > this.timeData.length) {
                index = this.timeData.length
              } else {
                index = index + 1
              }
              if (this.timeData[index].substring(0, 3) == '03_') {
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index - 1].substring(0, 3) == '03_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index - 1].substring(0, 3) == '04_') {
                index = index - 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('04_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }
            }
          }
        }       
      },
      current() { // 当天       
        let sameDayA = '01_' + standardDate(new Date());
        let sameDayB = '02_' + standardDate(new Date());
        let sameDayAn = '03_' + standardDate(new Date());
        let sameDayBn = '04_' + standardDate(new Date());
        let srcDate = this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 13);
        let user = JSON.parse(sessionStorage.getItem('user'));
        if (user.Content == '内科楼ICU') {
          if ($.inArray(sameDayAn, this.timeData) == -1 && $.inArray(sameDayBn, this.timeData) == -1) {
            this.$toast.center('该患者没有当天的PDF')
          } else {
            if ( sameDayAn == srcDate || sameDayBn == srcDate) {
              this.$toast.center('当前的PDF正是今天的')
            } else {
              if ($.inArray(sameDayAn, this.timeData) != -1 && $.inArray(sameDayBn, this.timeData) != -1 || $.inArray(sameDayAn, this.timeData) != -1 && $.inArray(sameDayBn, this.timeData) == -1) {
                let srcString = axios.defaults.baseURL.replace("api", "") + this.nursingData[$.inArray(sameDayAn, this.timeData)].MemoryAddress.replace("AppServerWeb", "");
                this.src = srcString.replace(srcDate, sameDayAn);
                let type = sameDayAn.replace('03_', '_A面').substring(0, 3);
                let date = sameDayAn.substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if ($.inArray(sameDayBn, this.timeData) != -1 && $.inArray(sameDayAn, this.timeData) == -1) {
                let srcString = axios.defaults.baseURL.replace("api", "") + this.nursingData[$.inArray(sameDayBn, this.timeData)].MemoryAddress.replace("AppServerWeb", "");
                this.src = srcString.replace(srcDate, sameDayBn);
                let type = sameDayBn.replace('04_', '_B面').substring(0, 3);
                let date = sameDayBn.substring(3, 20);
                this.nursingStr = date.concat(type);
              }
            }

          } 
        } else if (user.Content == '外科楼ICU') {
          if ($.inArray(sameDayA, this.timeData) == -1 && $.inArray(sameDayB, this.timeData) == -1) {
            this.$toast.center('该患者没有当天的PDF')
          } else {
            if (sameDayA == srcDate || sameDayB == srcDate ) {
              this.$toast.center('当前的PDF正是今天的')
            } else {
              if ($.inArray(sameDayA, this.timeData) != -1 && $.inArray(sameDayB, this.timeData) != -1 || $.inArray(sameDayA, this.timeData) != -1 && $.inArray(sameDayB, this.timeData) == -1) {
                let srcString = axios.defaults.baseURL.replace("api", "") + this.nursingData[$.inArray(sameDayA, this.timeData)].MemoryAddress.replace("AppServerWeb", "");
                this.src = srcString.replace(srcDate, sameDayA);
                let type = sameDayA.replace('01_', '_A面').substring(0, 3);
                let date = sameDayA.substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if ($.inArray(sameDayB, this.timeData) != -1 && $.inArray(sameDayA, this.timeData) == -1) {
                let srcString = axios.defaults.baseURL.replace("api", "") + this.nursingData[$.inArray(sameDayB, this.timeData)].MemoryAddress.replace("AppServerWeb", "");
                this.src = srcString.replace(srcDate, sameDayB);
                let type = sameDayB.replace('02_', '_B面').substring(0, 3);
                let date = sameDayB.substring(3, 20);
                this.nursingStr = date.concat(type);
              } 
            }

          } 
        }
       
      },
      next() { //后一天
        let srcDate = this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 13);
        let type = this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 2);
        let index = $.inArray(srcDate, this.timeData);
        //console.log(this.timeData[index + 1].substring(0, 3) == '02_')
        if (index != -1) {
          if (index == 0) {
            this.$toast.center('该患者没有更晚的PDF')
          } else {
            if (type == '01') {
              if (index - 2 < 0)
              {
                index = 0
              } else {
                index = index - 2;
              }
              if (this.timeData[index].substring(0, 3) == '01_') {
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '01_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '02_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('02_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }
           
            } else if (type == '02') {
              if (index - 3 < 0) {
                index = 0
              } else {
                index = index - 3;
              }
              if (this.timeData[index].substring(0, 3) == '01_') {
              this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
              let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
              let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '01_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('01_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '02_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('02_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }
            } else if (type == '03') {
              if (index - 2 < 0) {
                index = 0
              } else {
                index = index - 2;
              }
              //console.log(this.timeData[index].substring(0, 3))
              if (this.timeData[index].substring(0, 3) == '03_') {
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '03_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '04_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('04_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }

            } else if (type == '04') {
              if (index - 3 < 0) {
                index = 0
              } else {
                index = index - 3;
              }
              if (this.timeData[index].substring(0, 3) == '03_') {
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '03_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('03_', '_A面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              } else if (this.timeData[index + 1].substring(0, 3) == '04_') {
                index = index + 1;
                this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[index].MemoryAddress.replace("AppServerWeb", "");
                let type = this.timeData[index].replace('04_', '_B面').substring(0, 3);
                let date = this.timeData[index].substring(3, 20);
                this.nursingStr = date.concat(type);
              }
            }
          }
        }
      },
      nursingA() {
        let srcDate = this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 13);
        let indW = $.inArray(srcDate.replace('02_', '01_'), this.timeData);//外科楼
        let indN = $.inArray(srcDate.replace('04_', '03_'), this.timeData);   //内科楼
        let user = JSON.parse(sessionStorage.getItem('user'));
       //当在外科楼时
        if (user.Content == '外科楼ICU') {
          if (indW != -1) {
            if (this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 2) == '02') {
              let type = this.timeData[indW].replace('01_', '_A面').substring(0, 3);
              let date = this.timeData[indW].substring(3, 20);
              this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[indW].MemoryAddress.replace("AppServerWeb", "");
              this.nursingStr = date.concat(type);
            } else {
              this.$toast.center('当前就在A面')
            }
          } else {
            this.$toast.center('当前患者没有A面')
          }
        }
       //当在内科楼时
        if (user.Content == '内科楼ICU') {
          if (indN != -1) {
            if (this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 2) == '04') {
              let type = this.timeData[indN].replace('03_', '_A面').substring(0, 3);
              let date = this.timeData[indN].substring(3, 20);
              this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[indN].MemoryAddress.replace("AppServerWeb", "");
              this.nursingStr = date.concat(type);
            } else {
              this.$toast.center('当前就在A面')
            }

          } else {
            this.$toast.center('当前患者没有A面')
          }
        }
        
      },
      nursingB() {
        let srcDate = this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 13);
        let indW = $.inArray(srcDate.replace('01_', '02_'), this.timeData);   //外科楼
        let indN = $.inArray(srcDate.replace('03_', '04_'), this.timeData);   //内科楼
        let user = JSON.parse(sessionStorage.getItem('user'));
        //当在外科楼时
        if (user.Content == '外科楼ICU') {
          if (indW != -1) {
            if (this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 2) == '01') {
              let type = this.timeData[indW].replace('02_', '_B面').substring(0, 3);
              let date = this.timeData[indW].substring(3, 20);
              this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[indW].MemoryAddress.replace("AppServerWeb", "");
              this.nursingStr = date.concat(type);
            } else {
              this.$toast.center('当前就在B面')
            }
          } else {
            this.$toast.center('当前患者没有B面')
          }
        }
        //当在内科楼时
        if (user.Content == '内科楼ICU') {
          if (indN != -1) {
            if (this.src.substring(this.src.indexOf('圳') + 1, 100).substring(0, 2) == '03') {
              let type = this.timeData[indN].replace('04_', '_B面').substring(0, 3);
              let date = this.timeData[indN].substring(3, 20);
              this.src = axios.defaults.baseURL.replace("api", "") + this.nursingData[indN].MemoryAddress.replace("AppServerWeb", "");
              this.nursingStr = date.concat(type);

            } else {
              this.$toast.center('当前就在B面')
            }
          } else {
            this.$toast.center('当前患者没有B面')
          }
        }
      }
    },
    filters: {
      filterStr(str) {
        if (!str) {
          return ''
        } else {
          str = str.substring(str.indexOf('圳') + 1, 28);
          if (str.indexOf('01_') != -1) {
            let type = str.replace('01_', '_A面').substring(0, 3);
            let date = str.substring(3, 20);
            return date.concat(type); 
          } else if (str.indexOf('02_') != -1) {
            let type = str.replace('02_', '_B面').substring(0, 3);
            let date = str.substring(3, 20);
            return date.concat(type); 
          } else if (str.indexOf('03_') != -1) {
            let type = str.replace('03_', '_A面').substring(0, 3);
            let date = str.substring(3, 20);
            return date.concat(type);
          } else if (str.indexOf('04_') != -1) {
            let type = str.replace('04_', '_B面').substring(0, 3);
            let date = str.substring(3, 20);
            return date.concat(type);
          }
        }
      }
    }
  
}
</script>
<style scoped>
  #nursingPdf{
    width:100%;
    height:100vh;
  }
  .nursingTopHeader {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    width: 100%;
    background-color: #fff;
    z-index: 9999;
  }
  #nursingPdf .backTo {
  }
    #nursingPdf .backTo img{
      width:42px;
    }
  .dropDown{
    margin: 10px 0 10px 80px;
    width:180px;
    height:34px;
    line-height:34px;
    background:#fff;
    padding-left:10px;
   border:1px solid #e5e5e5;
  }
    .dropDown p {
      color: #999;
      position: relative;
    }

      .dropDown p:after {
        display: inline-block;
        content: '';
        width: 10px;
        height: 10px;
        background-image: url("../../../static/images/unfold_arron.png");
        background-repeat: no-repeat;
        background-size: contain;
        vertical-align: middle;
        margin-right: 8px;
        margin-top:14px;
        float:right;
      }

      .dropDown p.up:after {
        background-image: url("../../../static/images/up_arron.png");
      }
  .nursingButton{
    margin: 10px 0 10px 30px;
  }
    .nursingButton button{
      background-color:#fff;
      padding:6px 15px;
      border:1px solid #e5e5e5;
    }
  .nursingAorB {
    margin: 10px 0 10px 30px;
  }
    .nursingAorB button {
      background-color: #fff;
      padding: 6px 15px;
      border: 1px solid #e5e5e5;
    }
    .list-link {
      position: relative;
      z-index: 9999;
      display: none;
      border: 1px solid #eee;
      left: -10px;
      width: 240px;
      background-color: #fff;
      box-shadow: 2px 5px 5px #ccc;
      -webkit-column-count: 2;
      -moz-column-count: 2;
      column-count: 2;
    }
    .list-link li {
      text-align: center;
      cursor: pointer;
    }

      .list-link li:hover {
        background-color: #f6f6f6;
        border-bottom: 1px solid #ccc;
      }
</style>
